Various modernizations in humminbird. (#1100)
authorRobert Lipe <robertlipe@users.noreply.github.com>
Sat, 14 Sep 2024 04:18:52 +0000 (23:18 -0500)
committerGitHub <noreply@github.com>
Sat, 14 Sep 2024 04:18:52 +0000 (23:18 -0500)
* Various modernizations in humminbird.

- Make humminbird_icons[] a QStringList.
- I don't think the icon finder ever actually worked the way it was described in the
comments.
- Comment fix in exif
- Kill xstrndup (yay)

.gitignore
defs.h
exif.cc
humminbird.cc
humminbird.h
util.cc

index eb7338ca291cf3abcc7aace3ff8002e84fc77ab9..0aecc86f8dc35959c140d68ce4a1a8b2ea1ae133 100644 (file)
@@ -1,6 +1,7 @@
 *.a
+*.cbp
 *.o
-*.swp
+*.swo
 .ninja_deps
 .ninja_log
 .qmake.cache
@@ -10,6 +11,7 @@
 /*.gcno
 /*.gcov
 /.cmake/
+/.idea/
 /.vs/
 /.vscode/
 /GPSBabel
@@ -27,6 +29,7 @@
 /build/
 /codacy-clang-tidy
 /compile_commands.json
+/config.tests/
 /config.h
 /config.log
 /config.status
diff --git a/defs.h b/defs.h
index 0902154b997bfc16ed4ee9e9c119f4e866e6d294..73dd84ed3c40293ecec5e9aac558fa85093442a4 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -922,7 +922,6 @@ void* xmalloc(size_t size);
 void* xrealloc(void* p, size_t s);
 void xfree(const void* mem);
 char* xstrdup(const QString& s);
-char* xstrndup(const char* str, size_t sz);
 char* xstrdup(const char* s);
 
 FILE* xfopen(const char* fname, const char* type, const char* errtxt);
diff --git a/exif.cc b/exif.cc
index 553a4775bc13c12bb4ac9687850d04792ceb1dbf..cece53d16fd4daa9253833fcaad3fefd2ff68ec2 100644 (file)
--- a/exif.cc
+++ b/exif.cc
@@ -63,7 +63,7 @@
 #include <cstring>              // for memcmp, strlen
 #include <utility>              // for as_const
 
-#include "defs.h"               // for Waypoint, fatal, warning, global_options, global_opts, unknown_alt, xfree, route_disp_all, track_disp_all, waypt_disp_all, wp_flags, KNOTS_TO_MPS, KPH_TO_MPS, MPH_TO_MPS, MPS_TO_KPH, WAYPT_HAS, case_ignore_strcmp, waypt_add, xstrdup, xstrndup, fix_2d
+#include "defs.h"               // for Waypoint, fatal, warning, global_options, global_opts, unknown_alt, xfree, route_disp_all, track_disp_all, waypt_disp_all, wp_flags, KNOTS_TO_MPS, KPH_TO_MPS, MPH_TO_MPS, MPS_TO_KPH, WAYPT_HAS, case_ignore_strcmp, waypt_add, xstrdup, fix_2d
 #include "garmin_tables.h"      // for gt_lookup_datum_index
 #include "gbfile.h"             // for gbfputuint32, gbfputuint16, gbfgetuint16, gbfgetuint32, gbfseek, gbftell, gbfile, gbfclose, gbfcopyfrom, gbfwrite, gbfopen_be, gbfread, gbfrewind, gbfgetflt, gbfgetint16, gbfopen, gbfputc, gbfputflt, gbsize_t, gbfeof, gbfgetdbl, gbfputdbl, gbfile::(anonymous)
 #include "jeeps/gpsmath.h"      // for GPS_Math_WGS84_To_Known_Datum_M
index e6f7505f53da8ba8e839f310eea13454b8af5a55..05e379c4ac63d9d5eba812205b9bf021634d4626 100644 (file)
@@ -1,7 +1,7 @@
 /*
     Copyright (C) 2008  Björn Augustsson, oggust@gmail.com
     Copyright (C) 2008  Olaf Klein, o.b.klein@gpsbabel.org
-    Copyright (C) 2005-2013 Robert Lipe, robertlipe+source@gpsbabel.org
+    Copyright (C) 2005-2024 Robert Lipe, robertlipe+source@gpsbabel.org
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -62,8 +62,6 @@ Still, they're useful in the code as a plain signature.
 #define WPT_MAGIC2             0x02030024L // New for 2013.  No visible diff?!
 #define RTE_MAGIC              0x03030088L
 
-#define BAD_CHARS              "\r\n\t"
-
 /* The hwr data format is records-based, and the records are 36 bytes long. */
 
 struct HumminbirdBase::humminbird_waypt_t {
@@ -223,7 +221,8 @@ HumminbirdBase::humminbird_rd_deinit() const
 void
 HumminbirdBase::humminbird_read_wpt(gbfile* fin)
 {
-  humminbird_waypt_t w{};
+  humminbird_waypt_t w{0};
+  static_assert(sizeof(w) == 32);
 
   if (! gbfread(&w, 1, sizeof(w), fin)) {
     fatal(MYNAME ": Unexpected end of file!\n");
@@ -248,7 +247,7 @@ HumminbirdBase::humminbird_read_wpt(gbfile* fin)
   wpt->latitude = geocentric_to_geodetic_hwr(guder);
   wpt->longitude = static_cast<double>(w.east) / EAST_SCALE * 180.0;
 
-  wpt->altitude  = 0.0; /* It's from a fishfinder... */
+  wpt->altitude = 0.0; /* It's from a fishfinder... */
 
   if (w.depth != 0) {
     wpt->set_depth(static_cast<double>(w.depth) / 100.0);
@@ -284,8 +283,8 @@ HumminbirdBase::humminbird_read_wpt(gbfile* fin)
 void
 HumminbirdBase::humminbird_read_route(gbfile* fin) const
 {
-
-  humminbird_rte_t hrte{};
+  humminbird_rte_t hrte{0};
+  static_assert(sizeof(hrte) == 132);
 
   if (! gbfread(&hrte, 1, sizeof(hrte), fin)) {
     fatal(MYNAME ": Unexpected end of file!\n");
@@ -302,7 +301,7 @@ HumminbirdBase::humminbird_read_route(gbfile* fin) const
 
       /* locate the point over his internal Humminbird "Number" */
       if (const Waypoint* wpt = wpt_num_to_wpt_hash.value(hrte.points[i], nullptr); wpt != nullptr) {
-        if (rte == nullptr) {
+       if (rte == nullptr) {
           rte = new route_head;
           route_add_head(rte);
           rte->rte_name = QByteArray(hrte.name, static_cast<int>(qstrnlen(hrte.name, sizeof(hrte.name))));
@@ -316,8 +315,8 @@ HumminbirdBase::humminbird_read_route(gbfile* fin) const
 void
 HumminbirdBase::humminbird_read_track(gbfile* fin)
 {
-
-  humminbird_trk_header_t th{};
+  humminbird_trk_header_t th{0};
+  static_assert(sizeof(th) == 64);
 
   if (! gbfread(&th, 1, sizeof(th), fin)) {
     fatal(MYNAME ": Unexpected end of file reading header!\n");
@@ -424,12 +423,10 @@ HumminbirdBase::humminbird_read_track(gbfile* fin)
 void
 HumminbirdBase::humminbird_read_track_old(gbfile* fin)
 {
-
-  humminbird_trk_header_old_t th{};
+  humminbird_trk_header_old_t th{0};
   constexpr int file_len = 8048;
   char namebuf[TRK_NAME_LEN];
 
-
   if (! gbfread(&th, 1, sizeof(th), fin)) {
     fatal(MYNAME ": Unexpected end of file reading header!\n");
   }
@@ -483,7 +480,7 @@ HumminbirdBase::humminbird_read_track_old(gbfile* fin)
   first_wpt->altitude  = 0.0;
   track_add_wpt(trk, first_wpt);
 
-  for (int i = 0 ; i<th.num_points-1 ; i++) {
+  for (int i = 0 ; i < th.num_points-1 ; i++) {
     auto* wpt = new Waypoint;
 
     points[i].deltaeast  = be_read16(&points[i].deltaeast);
@@ -563,7 +560,7 @@ HumminbirdBase::humminbird_wr_init(const QString& fname)
   wptname_sh = new MakeShort;
 
   wptname_sh->set_length(WPT_NAME_LEN - 1);
-  wptname_sh->set_badchars(BAD_CHARS);
+  wptname_sh->set_badchars(kBadChars);
   wptname_sh->set_mustupper(false);
   wptname_sh->set_mustuniq(false);
   wptname_sh->set_whitespace_ok(true);
@@ -572,7 +569,7 @@ HumminbirdBase::humminbird_wr_init(const QString& fname)
 
   rtename_sh = new MakeShort;
   rtename_sh->set_length(RTE_NAME_LEN - 1);
-  rtename_sh->set_badchars(BAD_CHARS);
+  rtename_sh->set_badchars(kBadChars);
   rtename_sh->set_mustupper(false);
   rtename_sh->set_mustuniq(false);
   rtename_sh->set_whitespace_ok(true);
@@ -581,7 +578,7 @@ HumminbirdBase::humminbird_wr_init(const QString& fname)
 
   trkname_sh = new MakeShort;
   trkname_sh->set_length(RTE_NAME_LEN - 1);
-  trkname_sh->set_badchars(BAD_CHARS);
+  trkname_sh->set_badchars(kBadChars);
   trkname_sh->set_mustupper(false);
   trkname_sh->set_mustuniq(false);
   trkname_sh->set_whitespace_ok(true);
@@ -609,8 +606,7 @@ HumminbirdBase::humminbird_wr_deinit()
 void
 HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt)
 {
-  humminbird_waypt_t hum{};
-  int num_icons = std::size(humminbird_icons);
+  humminbird_waypt_t hum{0};
 
   be_write16(&hum.num, waypoint_num++);
   hum.zero   = 0;
@@ -618,20 +614,26 @@ HumminbirdFormat::humminbird_write_waypoint(const Waypoint* wpt)
   hum.icon   = 255;
 
   // Icon....
-  if (!wpt->icon_descr.isNull()) {
-    for (int i = 0; i < num_icons; i++) {
-      if (!wpt->icon_descr.compare(humminbird_icons[i], Qt::CaseInsensitive)) {
+  QString icon = wpt->icon_descr;
+  if (!icon.isEmpty()) {
+    int i = 0;
+    for (const auto& hi : humminbird_icons) {
+      if (!icon.compare(hi, Qt::CaseInsensitive)) {
         hum.icon = i;
         break;
       }
+      i++;
     }
-    if (hum.icon == 255) {     /* no success, no try to find the item in a more comlex name */
+
+    if (hum.icon == 255) { /* no success, now try to find the item in a more complex name */
       hum.icon = 0;    /* i.e. "Diamond" as part of "Diamond, Green" or "Green Diamond" */
-      for (int i = 0; i < num_icons; i++) {
-        if (wpt->icon_descr.contains(humminbird_icons[i], Qt::CaseInsensitive)) {
+      i = 0;
+      for (const auto& hi : humminbird_icons) {
+        if (icon.contains(hi, Qt::CaseInsensitive)) {
           hum.icon = i;
           break;
         }
+        i++;
       }
     }
   }
index ebc34d9da827065749ad8004740f3de65408984c..9892e9b2904a0707ebb2a054346ee34d3d507d4d 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <QHash>     // for QHash
 #include <QString>   // for QString
+#include <QStringList>   // for QString
 #include <QVector>   // for QVector
 
 #include <cstdint>   // for int32_t, uint32_t
@@ -52,13 +53,16 @@ protected:
   // constants related to position conversions.
   static constexpr double i1924_equ_axis = 6378388.0;
   static constexpr double EAST_SCALE = 20038297.0; /* this is i1924_equ_axis*pi */
+
   // static constexpr double i1924_polar_axis = 6356911.946;
   // We use a modified international 1924 ellipse with a different flattening,
   // defined by cos_ae = cos(angular eccentricity).
   static constexpr double cos_ae = 0.9966349016452;
   static constexpr double cos2_ae = cos_ae * cos_ae;
 
-  static constexpr const char* humminbird_icons[] = {
+  static constexpr auto kBadChars = "\r\n\t";
+
+  const QStringList humminbird_icons = {
     "Normal",       /*  0 */
     "House",        /*  1 */
     "Red cross",    /*  2 */
diff --git a/util.cc b/util.cc
index 7285c7467f0049e1cb45f0820de32c092e1b674e..7840d5268a16b8a67b479db661885e95ca374ca9 100644 (file)
--- a/util.cc
+++ b/util.cc
@@ -104,27 +104,6 @@ char* xstrdup(const QString& s)
   return xstrdup(CSTR(s));
 }
 
-/*
- * Duplicate at most sz bytes in str.
- */
-char*
-xstrndup(const char* str, size_t sz)
-{
-  size_t newlen = 0;
-  const char* cin = str;
-
-  while ((newlen < sz) && (*cin != '\0')) {
-    newlen++;
-    cin++;
-  }
-
-  char* newstr = (char*) xmalloc(newlen + 1);
-  memcpy(newstr, str, newlen);
-  newstr[newlen] = 0;
-
-  return newstr;
-}
-
 void*
 xrealloc(void* p, size_t s)
 {